<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="受保护IP" prop="ip">
        <el-input
          v-model.trim="queryParams.ip"
          placeholder="请输入受保护IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select
          v-model="queryParams.taskStatus"
          placeholder="请选择任务状态"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.atlas_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="抗归属方法" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择抗归属方法"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.atlas_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="taskList">
      <el-table-column label="受保护IP" align="center" prop="ip" />
      <el-table-column label="抗归属方法" align="center" prop="type">
        <template slot-scope="scope">
          <span
            v-for="dict in dict.type.atlas_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            ><span v-if="dict.value == scope.row.type">{{
              dict.label
            }}</span></span
          >
        </template>
      </el-table-column>
      <el-table-column label="域名" align="center" prop="domain" />
      <el-table-column label="抗归属前组织" align="center" prop="beforeRiskOrg" />
      <el-table-column label="抗归属后组织" align="center" prop="afterRiskOrg" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <!-- <el-table-column label="当前状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <span
            v-for="dict in dict.type.atlas_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            ><span v-if="dict.value == scope.row.taskStatus">{{
              dict.label
            }}</span></span
          >
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            v-if="scope.row.taskStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStop(scope.row)"
            >停止</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  selectCombatAtlasTaskListNew,
  deleteCombatAtlasTaskByIdNew,
  addCombatAtlasTask,
  stopCombatAtlasTask,
} from "@/api/IPOwnershipProtection/dataStatistics.js";

export default {
  name: "Task",
  dicts: ["atlas_task_status", "atlas_task_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网路IP保护任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      selectCombatAtlasTaskListNew(this.queryParams).then((res) => {
        
        this.taskList = res.rows.map(item => {
          if(item.after && item.type == '1'){
            let data =JSON.parse(item.after)
            item.afterRiskOrg = data.new_name
            item.targetIp = data.ip
            item.beforeRiskOrg = data.old_name
            item.domain = data.original_domain
            return item
          }else{
            let data =JSON.parse(item.content)
            //获取data.new_probabilities的key
            let key = Object.keys(data.new_probabilities)[0]
            let key1 = Object.keys(data.new_probabilities)[1]
            let key2 = Object.keys(data.original_probabilities)[1]

            item.afterRiskOrg = key
            item.beforeRiskOrg = key1?key1:key2

            return item
          }
        })
        // this.taskList = res.rows;
        // this.taskList = res.rows;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**停止按钮操作*/
    handleStop(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认停止"' + id + '"的任务？')
        .then(function () {
          return stopCombatAtlasTask(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("停止成功");
        })
        .catch(() => {});
    },
    /** 查看按钮操作 */
    handleDetail(row) {
      const id = row.id;
      this.$router.push('/deviceHomeScreen')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$modal
        .confirm('是否确认删除"' + ids + '"的数据项？')
        .then(function () {
          return deleteCombatAtlasTaskByIdNew(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
</style>